/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2024-05-06     liuchao       the first version
 */
#define LOG_TAG "relay"
#include "drv_relay.h"
#include <ulog.h>

int relay_init(relay_e relay)
{
    switch (relay)
    {
    case RELAY_PERIPH_PWR:
        rt_pin_mode(RELAY_PERIPH_PWR_PIN, PIN_MODE_OUTPUT);
        rt_pin_write(RELAY_PERIPH_PWR_PIN, PIN_HIGH);
        break;
        // 安全限制
    case RELAY_SAFE:
        rt_pin_write(RELAY_SAFE_PIN, PIN_LOW);
        rt_pin_mode(RELAY_SAFE_PIN, PIN_MODE_OUTPUT);
        rt_pin_write(RELAY_SAFE_PIN, PIN_LOW);
        break;
        // 自锁电机正转
    case RELAY_LOCK:
        rt_pin_write(RELAY_LOCK_PIN, PIN_LOW);
        rt_pin_mode(RELAY_LOCK_PIN, PIN_MODE_OUTPUT);
        rt_pin_write(RELAY_LOCK_PIN, PIN_LOW);
        break;
        // 自锁电机反转
    case RELAY_UNLOCK:
        rt_pin_write(RELAY_UNLOCK_PIN, PIN_LOW);
        rt_pin_mode(RELAY_UNLOCK_PIN, PIN_MODE_OUTPUT);
        rt_pin_write(RELAY_UNLOCK_PIN, PIN_LOW);
        break;
        // 液压站电源
    case RELAY_HYDRAULIC:
        rt_pin_write(RELAY_HYDRAULIC_PIN, PIN_LOW);
        rt_pin_mode(RELAY_HYDRAULIC_PIN, PIN_MODE_OUTPUT);
        rt_pin_write(RELAY_HYDRAULIC_PIN, PIN_LOW);
        break;
        // 液压站启动
    case RELAY_HY_START:
        rt_pin_write(RELAY_HY_START_PIN, PIN_LOW);
        rt_pin_mode(RELAY_HY_START_PIN, PIN_MODE_OUTPUT);
        rt_pin_write(RELAY_HY_START_PIN, PIN_LOW);
        break;
        // 推进电磁阀
    case RELAY_PUSH:
        rt_pin_write(RELAY_PUSH_PIN, PIN_LOW);
        rt_pin_mode(RELAY_PUSH_PIN, PIN_MODE_OUTPUT);
        rt_pin_write(RELAY_PUSH_PIN, PIN_LOW);
        break;
        // 回退电磁阀
    case RELAY_BACK:
        rt_pin_write(RELAY_BACK_PIN, PIN_LOW);
        rt_pin_mode(RELAY_BACK_PIN, PIN_MODE_OUTPUT);
        rt_pin_write(RELAY_BACK_PIN, PIN_LOW);
        break;
    default:
        break;
    }
    return RT_EOK;
}

void relay_off(relay_e relay)
{
    switch (relay)
    {
    // 外设电源
    case RELAY_PERIPH_PWR:
        rt_pin_write(RELAY_PERIPH_PWR_PIN, PIN_LOW);
        break;
        // 安全限制
    case RELAY_SAFE:
        rt_pin_write(RELAY_SAFE_PIN, PIN_LOW);
        break;
        // 自锁电机正转
    case RELAY_LOCK:
        rt_pin_write(RELAY_LOCK_PIN, PIN_LOW);
        break;
        // 自锁电机反转
    case RELAY_UNLOCK:
        rt_pin_write(RELAY_UNLOCK_PIN, PIN_LOW);
        break;
        // 液压站电源
    case RELAY_HYDRAULIC:
        rt_pin_write(RELAY_HYDRAULIC_PIN, PIN_LOW);
        break;
        // 液压站启动
    case RELAY_HY_START:
        rt_pin_write(RELAY_HY_START_PIN, PIN_LOW);
        break;
        // 推进电磁阀
    case RELAY_PUSH:
        rt_pin_write(RELAY_PUSH_PIN, PIN_LOW);
        break;
        // 回退电磁阀
    case RELAY_BACK:
        rt_pin_write(RELAY_BACK_PIN, PIN_LOW);
        break;
    default:
        LOG_E("Invalid relay %d", relay);
        break;
    }
}

void relay_on(relay_e relay)
{
    switch (relay)
    {
    // 外设电源
    case RELAY_PERIPH_PWR:
        rt_pin_write(RELAY_PERIPH_PWR_PIN, PIN_HIGH);
        break;
        // 安全限制
    case RELAY_SAFE:
        rt_pin_write(RELAY_SAFE_PIN, PIN_HIGH);
        break;
        // 自锁电机正转
    case RELAY_LOCK:
        rt_pin_write(RELAY_LOCK_PIN, PIN_HIGH);
        break;
        // 自锁电机反转
    case RELAY_UNLOCK:
        rt_pin_write(RELAY_UNLOCK_PIN, PIN_HIGH);
        break;
        // 液压站电源
    case RELAY_HYDRAULIC:
        rt_pin_write(RELAY_HYDRAULIC_PIN, PIN_HIGH);
        break;
        // 液压站启动
    case RELAY_HY_START:
        rt_pin_write(RELAY_HY_START_PIN, PIN_HIGH);
        break;
        // 推进电磁阀
    case RELAY_PUSH:
        rt_pin_write(RELAY_PUSH_PIN, PIN_HIGH);
        break;
        // 回退电磁阀
    case RELAY_BACK:
        rt_pin_write(RELAY_BACK_PIN, PIN_HIGH);
        break;
    default:
        LOG_E("Invalid relay %d", relay);
        break;
    }
}

const char* relay_name(relay_e relay)
{
    switch (relay)
    {
    // 外设电源
    case RELAY_PERIPH_PWR:
        return "PERIPH_PWR";
        // 安全限制
    case RELAY_SAFE:
        return "SAFE";
        // 自锁电机正转
    case RELAY_LOCK:
        return "LOCK";
        // 自锁电机反转
    case RELAY_UNLOCK:
        return "UNLOCK";
        // 液压站电源
    case RELAY_HYDRAULIC:
        return "HYDRAULIC";
        // 液压站启动
    case RELAY_HY_START:
        return "HY_START";
        // 推进电磁阀
    case RELAY_PUSH:
        return "PUSH";
        // 回退电磁阀
    case RELAY_BACK:
        return "BACK";
    default:
        return "UNKNOWN";
    }
}

rt_base_t relay_pin(relay_e relay)
{
    rt_base_t pin = 0;
    switch (relay)
    {
    // 外设电源
    case RELAY_PERIPH_PWR:
        pin = RELAY_PERIPH_PWR_PIN;
        break;
        // 安全限制
    case RELAY_SAFE:
        pin = RELAY_SAFE_PIN;
        break;
        // 自锁电机正转
    case RELAY_LOCK:
        pin = RELAY_LOCK_PIN;
        break;
        // 自锁电机反转
    case RELAY_UNLOCK:
        pin = RELAY_UNLOCK_PIN;
        break;
        // 液压站电源
    case RELAY_HYDRAULIC:
        pin = RELAY_HYDRAULIC_PIN;
        break;
        // 液压站启动
    case RELAY_HY_START:
        pin = RELAY_HY_START_PIN;
        break;
        // 推进电磁阀
    case RELAY_PUSH:
        pin = RELAY_PUSH_PIN;
        break;
        // 回退电磁阀
    case RELAY_BACK:
        pin = RELAY_BACK_PIN;
        break;
    default:
        break;
    }

    return pin;
}

int relay_state(relay_e relay)
{
    rt_base_t pin = relay_pin(relay);
    if (pin != 0)
    {
        if (rt_pin_read(pin) == PIN_HIGH)
        {
            return RELAY_STATE_ON;
        }
    }
    return RELAY_STATE_OFF;
}

static int init_relays()
{
    for (int relay = RELAY_SAFE; relay < RELAY_END; relay++)
    {
        relay_init(relay);
    }
    return RT_EOK;
}
INIT_COMPONENT_EXPORT(init_relays);
